const api = require('../../../ws_micro_shop/utils/api.js');
const app = getApp();
Page({
  data: {
    goodsDetail: {}, // 详情信息
    specsNameList: [], // 规格名字列表
    specsList: [], // 规格数据信息列表
    attrList: [],
  },
  onLoad(options) {
    let that = this;
    let opts = options || {};
    app.loadCurrencyUnit().then(res => {
      that.setData({
        unit: res.unit,
      });
    });
    this.setData({
      type: opts.type,
    });
    this.loadGoodsDetail(opts);
  },
  onShow() { 
    let pages = getCurrentPages();
    let pre_page = pages[pages.length - 2];
    pre_page.setData({
      ifRefresh: true,
    });
  },
  onHide() { },
  onUnload() { },
  loadGoodsDetail(opts) {
    let that = this;
    let type = that.data.type;
    app.ws_micro_shoprequest({
      url: api.default.goods,
      data: {
        id: opts.id || '',
      },
      success: function (res) {
        if (res.code === 0) {
          that.setData({
            goodsDetail: res.data,
            specsNameList: res.data.use_attr === 1 ? res.data.attr_group_list : [],
            specsList: res.data.use_attr === 1 ? res.data.attr_group_list2.map((item, index) => {
              const data = Object.assign({}, item, {
                name: item.name.split('-'),
                price: 0,
                sale_price: item.price,
                num: item.num,
                get_num: 0,
              });
              return data;
            }) : [],
            attrList: res.data.use_attr === 1 ? res.data.attr_group_list2.map((item, index) => {
              const data = Object.assign({}, item, {
                name: item.name,
                price: type ? item.price : 0,
                num: item.num,
                get_num: 0,
              });
              return data;
            }) : [],
          });
        }
      },
    });
  },
  wxmlTagATap(e) {
    let src = e.detail.src || false;
    let reUrl = /^\//;
    if (!src) {
      return;
    } else {
      if (reUrl.test(src)) {
        app.util.navigateTo('/ws_micro_shop' + src);
      } else {
        app.util.navigateTo('/ws_micro_shop/pages/web/web', { url: src });
      }
    }
  },
  inputeidtnum(e) {
    this.setData({
      [`attrList[${e.currentTarget.dataset.index}].get_num`]: Number(e.detail.value),
    });
  },
  inputeidtprice(e) {
    this.setData({
      [`attrList[${e.currentTarget.dataset.index}].price`]: Number(e.detail.value),
    });

  },
  inputnum(e) {
    this.setData({
      [`attrList[${e.currentTarget.dataset.index}].num`]: Number(e.detail.value),
    });

  },
  inputprice(e) {
    this.setData({
      [`attrList[${e.currentTarget.dataset.index}].price`]: Number(e.detail.value),
    });

  },
  releaseGoods(e) {
    let formdata = e.detail.value;
    if (this.data.goodsDetail.use_attr === 0) {
      if (!formdata.get_price) {
        wx.showToast({
          title: '拉取价不能为空',
          icon: 'none',
        });
        return;
      }
      if (Number(formdata.get_price) > Number(formdata.get_max_price) || Number(formdata.get_price) < Number(formdata.get_min_price)) {
        wx.showToast({
          title: '拉取价不在建议范围内',
          icon: 'none',
        });
        return;
      }
      if (!formdata.get_alarm) {
        wx.showToast({
          title: '拉取库存不能为空',
          icon: 'none',
        });
        return;
      }
      if (Number(formdata.get_alarm) > Number(formdata.goods_num)) {
        wx.showToast({
          title: '拉取库存超出库存',
          icon: 'none',
        });
        return;
      }
      if (Number(formdata.get_alarm) <= 0) {
        wx.showToast({
          title: '拉取库存不能小于等于0',
          icon: 'none',
        });
        return;
      }
      let attr = [{
        name: '默认',
        num: formdata.goods_num,
        price: formdata.get_price,
        get_num: formdata.get_alarm,
      }];
      this.setData({
        attrList: attr,
      });
      this.goodsPush();
    } else {
      if (!this.data.type) {
        let attr = this.data.attrList;
        let index = 0;
        for (let i in attr) {
          if (Number(attr[i].get_num) > 0) {
            if (Number(attr[i].price) > Number(formdata.get_max_price) || Number(attr[i].price) < Number(formdata.get_min_price)) {
              wx.showToast({
                title: '规格' + (Number(i) + 1) + '拉取价不在建议范围内',
                icon: 'none',
              });
              return;
            }
            if (Number(attr[i].get_num) > Number(attr[i].num)) {
              wx.showToast({
                title: '规格' + (Number(i) + 1) + '拉取库存超出库存',
                icon: 'none',
              });
              return;
            }
            if (Number(attr[i].get_num) < 0) {
              wx.showToast({
                title: '规格' + (Number(i) + 1) + '拉取库存不能小于0',
                icon: 'none',
              });
              return;
            }
          } else {
            index++;
          }
          if (index === this.data.attrList.length) {
            wx.showToast({
              title: '拉取库存不能都为0',
              icon: 'none',
            });
            return;
          }

          if (Number(i) + 1 === this.data.attrList.length) {
            this.goodsPush();
          }
        }
      } else {
        this.goodsPush();
      }
    }
    // return;
  },
  goodsPush() {
    wx.showLoading({
      title: '正在保存...',
      mask: true,
    });
    app.ws_micro_shoprequest({
      url: this.data.type ? api.adminer.goods_attr_edit : api.adminer.goodsPush,
      data: {
        goodsid: this.data.goodsDetail.id,
        shop_id: app.ws_micro_shop_globalData.shopid,
        user_attr: this.data.goodsDetail.use_attr,
        attr: JSON.stringify(this.data.attrList),
      },
      method: 'POST',
      success: function (resp) {
        wx.hideLoading();
        if (resp.code === 0) {
          // wx.redirectTo({
          //   url: '/ws_micro_shop_seller/pages/goods-get/index',
          // });
          wx.showModal({
            title: '提示',
            content: resp.msg,
            showCancel: false,
            confirmText: '确认',
            success: res => {
              if (res.confirm) {
                wx.navigateBack({
                  delta: 1,
                });
              }
            },
          });
        } else {
          wx.showToast({
            title: resp.msg,
            icon: 'none',
            duration: 3000,
          });
        }
      },
    });
  },
});
